<?php

class Mod_Default extends Mod{
    function index() {
        tpl("default/getFeeds");
    }

    function addObj() {
        if (!empty($_POST)) {
            $data['name'] = $_POST['name'];
            $data['uid'] = $this->uid;
            $data['username'] = $this->username;
            if (db()->entry->insert($data)) {
                $this->log(array('name' => $data['name'], 'act' => 'default:addObj'));
                msg( "添加成功了");
            } else {
                msg("添加失败了");
            }
        } else {
            $tpl['title'] = '添加你要比较的东东';
            tpl("default/addObj", $tpl);
        }
    }
    
    function multiAddObjAttr() {
    	if (!empty($_POST)) {
            foreach ($_POST['descrip'] as $name => $des) {
            	$data = db()->entry->findOne(array('name' => $name));
            	if (empty($data)) {
            		$data['name'] = $name;
            		$data['uid'] = $this->uid;
            		$data['username'] = $this->username;
            		db()->entry->insert($data);
            	}
	            $data['attrs'][] = array(
	                'field' => $_POST['field'],
	                'descrip' => $des
	            );
	            db()->entry->update(array('name' => $name), $data);
            }
            $url = url('default:addCompare?objs='.join(',', array_keys($_POST['descrip'])));
            msg('添加对比成功', array('url' => $url));
    	} else {
    		
    	}
    }

    function addObjAttr() {
        if (!empty($_POST)) {
            $data = db()->entry->findOne(array('name' => $_GET['name']));
            $data['attrs'][] = array(
                'field' => $_POST['field'],
                'descrip' => $_POST['descrip']
            );
            if (db()->entry->update(array('name' => $_GET['name']), $data)) {
                msg("添加说明成功", array('url' => url('default:showObj/name:'.$_GET['name'])));
            } else {
                msg("添加说明失败");
            }
        }
    }
	
	function addTag() {
		if (!empty($_POST)) {
			$obj = db()->entry->findOne(array('name' => $_GET['name']));
			if (!in_array($_POST['tag'], (array)$obj['tags'])) {
				$obj['tags'][] = $_POST['tag'];
				db()->entry->save($obj);
				msg('添加标签成功', array('url' => url('default:showObj/name:'.$_GET['name'])));
			}
		}
	}
	
	function addCompare() {
		$objs = explode(",", $_GET['objs']);
		$cur = db()->entry->find(array("name" => array('$in' => $objs)));
		$fields = array();
		$compare = array();
		$i = 0;
		foreach ($cur as $k => $entry) {
			foreach ($entry['attrs'] as $attr) {
				$fields[$attr['field']] = $attr['field'];
				$compare[$entry['name']][$attr['field']] = $attr['descrip'];
			}
			$i ++;
		}
		
		$args = array('fields' => $fields, 'objs' => $objs, 'compare' => $compare);
		tpl('default/addCompare', $args);
	}

    function getFeeds() {
        tpl("default/getFeeds");
    }

    function showObj() {
        $obj = db()->entry->findOne(array("name" => $_GET['name']));
        tpl("default/showObj", $obj);
    }
	/*
	function _merge_keys_merge_keys(&$obj1, &$obj2) {
		foreach ($obj1 as $k => $v) {
			if (empty($obj2[$k])) {
				$obj2[$k] = '';
			}
		}
	}
	*/
}